const {
	addParameter,
	importJSON,
	showParameterMenu,
	onDblClickRow,
	onClickRow,
	addParameterChildren,
	deleteParameter,
	saveJSONMethod,
	parameterChange,
} = require('./parameter-list-methods')

module.exports = function(templateComp) {
	let {
		parameterList,
		components,
		dailogComp,
		jsonText,
	} = templateComp
	let parameterMenu = components.parameterMenu
	let jsonDialog = dailogComp.jsonDialog
	parameterList.treegrid({
		title: '模板参数',
		fit: true,
		fitColumns: true,
		treeField: 'name',
		lines: true,
		idField: '_treeId_',
		dblclickToEdit: false,
		clickToEdit: false,
		columns: [
			[{
				field: 'name',
				title: '变量名',
				width: 60,
				editor: {
					type: 'textbox',
					options: {
						onChange(newValue, oldValue) {
							parameterChange.call(templateComp, newValue, oldValue, 'name')
						},
					}
				},
			}, {
				field: 'mandatory',
				title: '必填',
				width: 15,
				editor: {
					type: 'combobox',
					options: {
						valueField: 'id',
						textField: 'text',
						editable: false,
						data: [{
							id: 'M',
							text: "M"
						}, {
							id: 'O',
							text: "O"
						}],
						onChange(newValue, oldValue) {
							parameterChange.call(templateComp, newValue, oldValue, 'mandatory')
						},
					},
				},
			}, {
				field: 'content',
				title: '含义',
				width: 60,
				editor: {
					type: 'textbox',
					options: {
						onChange(newValue, oldValue) {
							parameterChange.call(templateComp, newValue, oldValue, 'content')
						},
					}
				},
			}, {
				field: 'type',
				title: '类型',
				width: 30,
				editor: {
					type: 'combobox',
					options: {
						valueField: 'id',
						textField: 'text',
						editable: false,
						data: [{
							id: 'string',
							text: "string"
						}, {
							id: 'number',
							text: "number"
						}, {
							id: 'boolean',
							text: "boolean",
						}, {
							id: 'array[string]',
							text: "array[string]",
						}, {
							id: 'array[number]',
							text: "array[number]",
						}, {
							id: 'array[boolean]',
							text: "array[boolean]",
						}, {
							id: 'array[object]',
							text: "array[object]",
						}, {
							id: 'object',
							text: "object",
						}],
						onChange(newValue, oldValue) {
							parameterChange.call(templateComp, newValue, oldValue, 'type')
						},
					},
				}
			}, {
				field: 'remark',
				title: '备注',
				width: 50,
				editor: {
					type: 'textbox',
					options: {
						onChange(newValue, oldValue) {
							parameterChange.call(templateComp, newValue, oldValue, 'remark')
						},
					}
				},
			}]
		],
		toolbar: [{
			iconCls: 'icon-add',
			text: '添加参数',
			handler: function() {
				addParameter.call(templateComp)
			}
		}, '-', {
			iconCls: 'icon-edit',
			text: '导入JSON',
			handler: function() {
				importJSON.call(templateComp)
			}
		}],

		/**
		 * 右键菜单
		 * @param  {[type]} e   [description]
		 * @param  {[type]} row [description]
		 * @return {[type]}     [description]
		 */
		onContextMenu(e, row) {
			showParameterMenu.call(templateComp, e, row)
		},

		/**
		 * 双击开始编辑行
		 * @param  {[type]} row [description]
		 * @return {[type]}     [description]
		 */
		onDblClickRow(row) {
			onDblClickRow.call(templateComp, row)
		},

		onClickRow(row) {
			onClickRow.call(templateComp, row)
		},
	})

	// 右键菜单
	parameterMenu.menu('appendItem', {
		id: 'addRequestParameterChildren',
		text: '添加属性',
		iconCls: 'icon-add',
		onclick(e) {
			addParameterChildren.call(templateComp, e)
		},
	}).menu('appendItem', {
		id: 'delRequestParameter',
		text: '删除属性',
		iconCls: 'icon-remove',
		onclick(e) {
			deleteParameter.call(templateComp)
		},
	});
	// 导入JSON对话框
	jsonDialog.dialog({
		title: 'JSON导入',
		top: 100,
		left: 300,
		width: 800,
		height: 460,
		closed: true,
		modal: true,
		buttons: [{
			text: '保存',
			iconCls: 'icon-save',
			handler: function() {
				saveJSONMethod.call(templateComp)
			}
		}, {
			text: '取消',
			iconCls: 'icon-cancel',
			handler: function() {
				jsonDialog.dialog('close')
			}
		}]
	});
	// JSON编辑框
	jsonText.textbox({
		multiline: true,
	});
}